/* global window */
import modelExtend from 'dva-model-extend'
import pathToRegexp from 'path-to-regexp'
import * as basicData from 'services/basicData.js'
import { phoneQuery, phoneEdit, phoneDel } from 'services/employer'
import { detailModel } from './common'
import { message } from 'antd'

export default modelExtend(detailModel, {
  namespace: 'employerPhoneData',

  state: {
    phoneType: '',
    parentId: '',
    contractTypeData: [],
    communicationTypeData: [],
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((pathname) => {
        const match = pathToRegexp('/employerphone/:id/:type').exec(pathname.pathname)
        if (match) {
          dispatch({ type: 'query', payload: { id: match[1], type: match[2] } })
          dispatch({ type: 'iniBasicData' })
          dispatch({ type: 'updateState', payload: { parentId: match[1], phoneType: match[2] } })
        }
      })
    },
  },

  effects: {
    * query ({ payload = {} }, { call, put }) {
      const data = yield call(phoneQuery, payload)
      let resData = {}
      if (data.success) {
        if (!data.data) {
          resData = {
            editType: 'create',
            data: {},
          }
        } else {
          resData = {
            editType: 'update',
            data: data.data,
          }
        }
        yield put({
          type: 'updateState',
          payload: { ...resData },
        })
      }
    },

    * delete ({ payload }, { call, put, select }) {
      const { parentId, phoneType } = yield select(_ => _.employerPhoneData)
      const data = yield call(phoneDel, { ...payload })
      if (data.success) {
        message.success('delete success')
        yield put({ type: 'query', payload: { id: parentId, type: phoneType } })
      } else {
        message.error('delete failed')
        throw data
      }
    },

    * update ({ payload }, { select, call, put }) {
      const { parentId, phoneType } = yield select(_ => _.employerPhoneData)
      payload.type = phoneType
      payload.personId = parentId
      const data = yield call(phoneEdit, payload)
      if (data.success) {
        message.success('update success')
        yield put({ type: 'query', payload: { id: parentId, type: phoneType } })
      } else {
        message.error('update failed')
        throw data
      }
    },


    * iniBasicData ({ payload = {} }, { call, put }) {
      const contractType = yield call(basicData.listall, { type: 'ContactType' })
      if (contractType.success) {
        yield put({
          type: 'updateState',
          payload: {
            contractTypeData: contractType.data,
          },
        })
      }
      const communicationType = yield call(basicData.listall, { type: 'CommunicationType' })
      if (communicationType.success) {
        yield put({
          type: 'updateState',
          payload: {
            communicationTypeData: communicationType.data,
          },
        })
      }
    },

  },

  reducers: {
  },
})
